package com.hxxy.wyp.petsLover.common.services.base.backend.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxxy.wyp.petsLover.common.entities.Good;
import com.hxxy.wyp.petsLover.common.entities.User;
import com.hxxy.wyp.petsLover.common.entities.vo.req.SearchObj;
import com.hxxy.wyp.petsLover.common.mapper.GoodMapper;
import com.hxxy.wyp.petsLover.common.services.base.backend.GoodService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @ClassName CommodityInfoServiceImpl
 * @Description TODO
 * @Author wyp
 * @Date 2023/1/17 22:51
 * @Version 1.0
 */
@Service
public class GoodServiceImpl extends ServiceImpl<GoodMapper, Good> implements GoodService {
    @Resource
    private GoodMapper goodMapper;

    @Override
    public Page pageSearch(SearchObj searchObj) {
        long page = Long.parseLong(searchObj.getPage());
        long pageSize = Long.parseLong(searchObj.getPageSize());
        //构造分页构造器
        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Good> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Strings.isNotBlank(searchObj.getId()), Good::getId, searchObj.getId());
        lambdaQueryWrapper.like(Strings.isNotBlank(searchObj.getGoodName()), Good::getGoodName, searchObj.getGoodName());
        lambdaQueryWrapper.like(Strings.isNotBlank(searchObj.getBrand()), Good::getBrand, searchObj.getBrand());
        lambdaQueryWrapper.eq(Strings.isNotBlank(searchObj.getGoodCategory()), Good::getCategory, searchObj.getGoodCategory());
        lambdaQueryWrapper.ge(searchObj.getBeginDate() != null, Good::getTimeOnShelves, searchObj.getBeginDate());
        lambdaQueryWrapper.le(searchObj.getEndDate() != null, Good::getTimeOnShelves, searchObj.getEndDate());
        lambdaQueryWrapper.ge(searchObj.getPriceBegin() != null, Good::getPrice, searchObj.getPriceBegin());
        lambdaQueryWrapper.le(searchObj.getPriceEnd() != null, Good::getPrice, searchObj.getPriceEnd());
        lambdaQueryWrapper.orderByDesc(Good::getTimeOnShelves);
        return goodMapper.selectPage(pageInfo,lambdaQueryWrapper);
    }
}
